package com.kylin.common.core.entity;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.OrderBy;
import com.baomidou.mybatisplus.annotation.TableField;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用树类数据实体类
 *
 * @author wuhao
 * @version 1.0 - 2019-02-13
 */
public abstract class TreeEntity<T extends TreeEntity<?>> extends DataEntity<T>{

    /** 树根节点编号 */
    public static final Long ROOT_CODE = 0L;
    /** 排序步长 */
    public static final int TREE_SORTS_LENGTH = 10;
    /** 默认排序值 */
    public static final int DEFAULT_TREE_SORT = 30;

    /** 父节点编号 */
    protected Long parentId;

    /** 所有父节点编号，逗号分隔*/
    protected String parentIds;

    /** 树排序 */
    protected Integer treeSort;

    /** 所有父节点排序，逗号分隔*/
    @OrderBy(asc = true, sort = 1)
    protected String treeSorts;

    /** 全节点名,包含自己 */
    protected String treeLabels;

    /** 子节点列表 */
    @TableField(exist =  false)
    @JSONField(serializeFeatures = {JSONWriter.Feature.NotWriteEmptyArray})
    private List<T> children = new ArrayList<>();

    protected TreeEntity() {}

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    /**
     * 获取节点名称，方便前端树插件使用
     * @return
     * @since  1.3.0  by wuhao at 2020/9/2
     */
    public abstract String getLabel();

    public Integer getTreeSort() {
        return treeSort;
    }

    public void setTreeSort(Integer treeSort) {
        this.treeSort = treeSort;
    }

    public String getTreeSorts() {
        return treeSorts;
    }

    public void setTreeSorts(String treeSorts) {
        this.treeSorts = treeSorts;
    }

    public String getTreeLabels() {
        return treeLabels;
    }

    public void setTreeLabels(String treeLabels) {
        this.treeLabels = treeLabels;
    }


    public List<T> getChildren() {
        return children;
    }

    public void setChildren(List<T> children) {
        this.children = children;
    }
}
